package de.acagamics.roteschlupfmuetze.core;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

/**
 * 
 * @author Andreas Reich BWHAHAHAHAHAHA
 *
 */
public class ImageHelper {
	
	/**
	 * scales an buffered image and returns the image as... BUFFERED IMAGE :D
	 * @param image incoming buffered image for scaling
	 * @param newWidth 	new width
	 * @param newHeight new height
	 * @return a new scaled image
	 */
	public static BufferedImage scaleImage(BufferedImage image, int newWidth, int newHeight) {
		// scale
		Image scaledImage = image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
		
		// buffer 
		BufferedImage scaledImageBuffer;
		if(scaledImage instanceof BufferedImage) {
			scaledImageBuffer = (BufferedImage)scaledImage;
		} else {
	        scaledImageBuffer = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
		    Graphics2D g = scaledImageBuffer.createGraphics();
		    g.drawImage(scaledImage, 0, 0, null);
		    g.dispose();
		}

		return scaledImageBuffer;
	}
	
	static BufferedImage rotateImage(BufferedImage image, double theta) {
		Image im = image.getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_SMOOTH);
		
		BufferedImage b = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = b.createGraphics();
		g.rotate(theta);
		g.drawImage(im, 0, 0, null);
		g.dispose();
		
		return b;
		
	}
	
	static BufferedImage rotateAndScaleImage(BufferedImage image, double theta, int newWidth, int newHeight) {
		// scale
		Image scaledImage = image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
		
		// buffer 
		BufferedImage scaledImageBuffer;
		if(scaledImage instanceof BufferedImage) {
			scaledImageBuffer = (BufferedImage)scaledImage;
		} else {
	        scaledImageBuffer = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
		    Graphics2D g = scaledImageBuffer.createGraphics();
		    g.rotate(theta);
		    g.drawImage(scaledImage, 0, 0, null);
		    g.dispose();
		}

		return scaledImageBuffer;
		
	}
}
